<template>
	<view>
		<image src="../../static/index/top.png" mode="widthFix" class="logo"></image>
		<view class="desc">
			<text class="name">欢迎使用「鹏海代驾」（司机端）</text>
		</view>
		<button class="btn" @tap="login">微信登陆</button>
		<view class="register-container">
			还未申请成为正式司机?
			<text class="link" @tap="toRegisterPage()">立即报名注册</text>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	export default {
		data() {
			return {

			};
		},
		methods: {
			toRegisterPage: function() {
				uni.navigateTo({
					url: '../register/register'
				});
			},
			async login() {
				// 1.获取临时授权code 
				const wxloginRes = await this.loginAsync();
				console.log("临时授权code: ", wxloginRes);
				// 2.组装请求数据
				const data = {
					code: wxloginRes.code
				};
				// 3.发起请求
				const loginResult = await this.requestAsync(this.urls.login, 'POST', data);
				console.log("登录响应: ", loginResult);

				//4 请求处理 
				await this.handleLoginResult(loginResult.data);
			},
			async handleLoginResult(data) {
				// 判断有没有注册/token 
				if (!data.hasOwnProperty('token')) {
					// 提示注册
					this.showErrorMessage("请先注册");
				} else {
					let token = data.token;
					let realAuth = data.realAuth;
					let faceAuth = data.faceAuth;
					uni.setStorageSync('token', token);
					uni.setStorageSync('realAuth', realAuth);
					//提示登录成功
					await this.showLoginSuccessToast();
					// 根据司机的认证状态判定跳转到那个页面
					await this.handleLoginSuccessAciton(realAuth, faceAuth);
				}
			},
			async showLoginSuccessToast() {
				return new Promise(resolve => {
					this.$refs.uToast.show({
						title: '登陆成功',
						type: 'success',
						message: '登陆成功',
						complete: resolve
					});
				});
			},
			async handleLoginSuccessAciton(realAuth, faceAuth) {
				// 如果没有实名认证
				if (realAuth == "1") {
					await this.redirectToAuth();
				} else if (faceAuth == "0") {
					// 否则如果还没有录入人脸, 弹窗提示
					const result = await this.showModalForFaceData();
					// 弹窗点击确定
					if (result.confirm) {
						// 进入人脸识别
						await this.redirectToFaceCamera();
					} else {
						// 否则进入工作台, 但是不能接单
						console.log("登录成功 进入工作台页面");
						await this.switchTabToHome();
					}

				} else {
					// 否则直接进入工作台 
					console.log("登录成功 进入工作台页面");
					await this.switchTabToHome();
				}
			},
			async showModalForFaceData() {
				return new Promise(resolve => {
					uni.showModal({
						title: '提示消息',
						content: '您还没有录入用于核实身份的面部特征信息，如果不录入将无法接单',
						confirmText: '录入',
						cancelText: '取消',
						success: resolve
					});
				});
			},
			async redirectToFaceCamera() {
				return new Promise(resolve => {
					uni.redirectTo({
						url: '../face/face?mode=create',
						success: resolve,
						fail: resolve
					});
				});
			},
			async redirectToAuth() {
				return new Promise(resolve => {
					uni.redirectTo({
						url: '../auth/auth?mode=create',
						success: resolve,
						fail: resolve
					});
				});
			},
			async switchTabToHome() {
				return new Promise(resolve => {
					uni.switchTab({
						url: '../home/home',
						success: resolve,
						fail: resolve
					});
				});
			},
		}
	}
</script>

<style lang="less">
	@import url("index.less");
</style>